
import dearpygui.dearpygui as dpg

# ui/base_window.py
class BaseWindow:
    """
    所有窗口的基础类，支持子元素添加与布局管理
    """

    def __init__(self, label, width=800, height=600):
        self.label = label
        self.width = width
        self.height = height
        self.children = []

    def add_child(self, child):
        """添加子组件"""
        self.children.append(child)

    def render(self):
        """渲染自身及所有子组件"""
        with dpg.window(label=self.label, width=self.width, height=self.height, tag=self.label):
            for child in self.children:
                child.render()


# ui/menu_bar.py
class MenuBar:
    """
    顶部菜单栏：文件、编辑、乐曲、导出、主题、插件、帮助
    """

    def render(self):
        with dpg.menu_bar():
            dpg.add_menu(label="文件")
            dpg.add_menu_item(label="新建", callback=lambda: print("新建"))
            dpg.add_menu_item(label="打开", callback=lambda: print("打开"))
            dpg.add_menu_item(label="保存", callback=lambda: print("保存"))

            dpg.add_menu(label="编辑")
            dpg.add_menu_item(label="撤销", callback=lambda: print("撤销"))
            dpg.add_menu_item(label="重做", callback=lambda: print("重做"))

            dpg.add_menu(label="乐曲")
            dpg.add_menu_item(label="播放", callback=lambda: print("播放"))
            dpg.add_menu_item(label="停止", callback=lambda: print("停止"))

            dpg.add_menu(label="导出")
            dpg.add_menu_item(label="导出为 XML", callback=lambda: print("导出 XML"))
            dpg.add_menu_item(label="导出为 MIDI", callback=lambda: print("导出 MIDI"))

            dpg.add_menu(label="主题")
            dpg.add_menu_item(label="深色", callback=lambda: print("切换深色"))
            dpg.add_menu_item(label="浅色", callback=lambda: print("切换浅色"))

            dpg.add_menu(label="插件")
            dpg.add_menu_item(label="加载插件", callback=lambda: print("加载插件"))

            dpg.add_menu(label="帮助")
            dpg.add_menu_item(label="关于", callback=lambda: print("关于"))


# ui/sidebar.py
class Sidebar:
    """
    左侧菜单栏：音轨属性、音符属性、乐曲属性、资源管理、插件管理、软件设置
    使用按钮点击切换内容区域
    """

    def __init__(self):
        self.active_tab = "track_props"

    def render(self):
        with dpg.child_window(width=150, height=-1, tag="sidebar"):
            with dpg.group(horizontal=True):
                dpg.add_button(label="音轨属性", callback=self._switch_to_track_props)
                dpg.add_button(label="音符属性", callback=self._switch_to_note_props)
                dpg.add_button(label="乐曲属性", callback=self._switch_to_song_props)
                dpg.add_button(label="资源管理", callback=self._switch_to_resource)
                dpg.add_button(label="插件管理", callback=self._switch_to_plugins)
                dpg.add_button(label="软件设置", callback=self._switch_to_settings)

            # 内容显示区
            with dpg.child_window(height=600, tag="sidebar_content"):
                dpg.add_text("菜单内容")

    def _switch_to_track_props(self):
        self.active_tab = "track_props"
        dpg.set_value("sidebar_content", "音轨属性内容")

    def _switch_to_note_props(self):
        self.active_tab = "note_props"
        dpg.set_value("sidebar_content", "音符属性内容")

    def _switch_to_song_props(self):
        self.active_tab = "song_props"
        dpg.set_value("sidebar_content", "乐曲属性内容")

    def _switch_to_resource(self):
        self.active_tab = "resource"
        dpg.set_value("sidebar_content", "资源管理内容")

    def _switch_to_plugins(self):
        self.active_tab = "plugins"
        dpg.set_value("sidebar_content", "插件管理内容")

    def _switch_to_settings(self):
        self.active_tab = "settings"
        dpg.set_value("sidebar_content", "软件设置内容")


# ui/track_panel.py
class TrackPanel:
    """
    音轨面板：显示所有音轨，支持增删改
    数据由 Project 提供
    """

    def __init__(self, project):
        self.project = project
        self.tracks = []  # 存储音轨 UI 元素标签

    def render(self):
        with dpg.child_window(width=-1, height=100, tag="track_panel"):
            dpg.add_text("编曲音轨")

            # 添加音轨示例
            for i, track in enumerate(self.project.tracks):
                with dpg.group(horizontal=True):
                    dpg.add_text(track.name, width=150)
                    dpg.add_button(label="M", width=20, tag=f"mute_{i}")
                    dpg.add_button(label="S", width=20, tag=f"solo_{i}")
                    dpg.add_slider_float(label="", min_value=0.0, max_value=1.0, width=60, tag=f"volume_{i}")
                    dpg.add_button(label="删除", width=40, callback=lambda sender, app_data, user_data=i: self.remove_track(user_data))


    def remove_track(self, index):
        """移除指定索引的音轨"""
        self.project.remove_track(index)
        self.render()  # 重新渲染（实际应优化为局部更新）


# ui/piano_roll.py
class PianoRoll:
    """
    钢琴卷帘：显示音符在时间轴上的分布
    可拖拽创建/编辑音符
    """

    def __init__(self, project):
        self.project = project

    def render(self):
        with dpg.child_window(width=-1, height=400, tag="piano_roll"):
            dpg.add_text("钢琴卷帘")

            # 模拟音符块
            for note in self.project.notes:
                with dpg.drawlist(width=1000, height=100):
                    dpg.draw_rectangle(
                        pmin=(note.time * 10, note.pitch * 20),
                        pmax=((note.time + note.duration) * 10, (note.pitch + 1) * 20),
                        color=(0, 255, 0, 100),
                        thickness=1,
                    )

            # 参数输入框
            with dpg.group():
                dpg.add_text("参数：音量")
                dpg.add_input_float(label="", default_value=0.7, width=100, tag="volume_param")

